package com.tianzeculture.stagebao.activity.home.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.tianzeculture.stagebao.R;

import java.util.List;

/**
 * Created by Administrator on 2018/6/1 0001.
 */

public class SearchHistoryAdapter extends BaseAdapter {
    Context context;
    List<String> strings;

    public SearchHistoryAdapter(Context context, List<String> strings) {
        this.context = context;
        this.strings = strings;
    }

    @Override
    public int getCount() {
        return strings.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null) {
            holder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.item_search_history, null);
            holder.tvHistory = (TextView) view.findViewById(R.id.tvHistory);
            holder.ibtnClean = (ImageButton) view.findViewById(R.id.ibtnClean);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        setData(holder, i);
        return view;
    }

    private void setData(ViewHolder holder, final int i) {
        holder.tvHistory.setText(strings.get(i));
        holder.ibtnClean.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                strings.remove(i);
                notifyDataSetChanged();
            }
        });
    }

    private class ViewHolder {
        TextView tvHistory;
        ImageButton ibtnClean;
    }
}
